package com.yxhl.zoume.core.tripsmgmt.presenter.home;

import android.content.Context;

import com.yxhl.protobuf.BizOrder;
import com.yxhl.protobuf.BizOrderRequest;
import com.yxhl.protobuf.BizOrderResponse;
import com.yxhl.protobuf.ClientType;
import com.yxhl.protobuf.CommonReqParams;
import com.yxhl.protobuf.CommonRespParams;
import com.yxhl.protobuf.PageOption;
import com.yxhl.protobuf.TravelType;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.common.ui.view.UpPullRefreshView;
import com.yxhl.zoume.core.tripsmgmt.presenter.ipresenter.ICommentPresenter;
import com.yxhl.zoume.core.tripsmgmt.view.home.HistoryTripsView;
import com.yxhl.zoume.core.tripsmgmt.view.unit.CommentOnDriverView;
import com.yxhl.zoume.data.http.rest.param.tripsmgnt.CommentDriverParam;
import com.yxhl.zoume.data.http.rest.response.tirps.CommentDriverResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.tripsmgmt.CommentDriverUseCase;
import com.yxhl.zoume.domain.interactor.tripsmgmt.HistoryTripOrdersUseCase;

import java.util.List;

import javax.inject.Inject;

import rx.Subscription;

/**
 * 项目名称：Ferrari
 * Created by menggod on 16/11/29 .
 */
public class HistoryTripPresenter extends BasePresenter implements ICommentPresenter {

    /**
     * 一次上拉加载的item条目数
     */
    public static final int PAGE_SIZE = 15;
    private int currentPage = 1;

    private static final String TAG = "HistoryTripPresenter";
    @Inject
    HistoryTripOrdersUseCase mHistoryTripOrdersUseCase;
    @Inject
    CommentDriverUseCase mCommentDriverUseCase;

    private HistoryTripsView mHistoryTripsView;
    private CommentOnDriverView mCommentOnDriverView;
    private UpPullRefreshView<BizOrder> mUpPullRefreshView;
    private Subscription mSubscribe;


    @Inject
    public HistoryTripPresenter(Context context) {
        super(context);
    }

    private BizOrderRequest getBizOrderRequest(TravelType travelType, boolean isRefresh) {
        if (isRefresh) {
            currentPage = 1;
        }
        CommonReqParams.Builder commonReqParamsBuilder = CommonReqParams.newBuilder();
        commonReqParamsBuilder.setClientType(ClientType.USER).setToken(getToken());

        PageOption.Builder pageOptionBuilder = PageOption.newBuilder();
        pageOptionBuilder.setCurrentPage(currentPage++).setPageSize(PAGE_SIZE);

        BizOrderRequest.Builder bizOrderRequestBuilder = BizOrderRequest.newBuilder();
        bizOrderRequestBuilder.setCommonReqParams(commonReqParamsBuilder)
                .setPageOption(pageOptionBuilder)
                .setType(travelType)
                .setMobile(getMobile());

        return bizOrderRequestBuilder.build();
    }


    public void queryHistoryTripsData(boolean isRefresh) {
        BizOrderRequest orderRequest = getBizOrderRequest(TravelType.TT2_HISTORY, isRefresh);
        mHistoryTripOrdersUseCase.execute(orderRequest, new ZMSubscriber<BizOrderResponse>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
                mHistoryTripsView.closeDownRefresh();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mHistoryTripsView.closeDownRefresh();
                mHistoryTripsView.renderDataOnError();
            }

            @Override
            public void onNext(BizOrderResponse bizOrderResponse) {
                super.onNext(bizOrderResponse);
                CommonRespParams commonRespParams = bizOrderResponse.getCommonRespParams();
                String isSucc = commonRespParams.getIsSucc();
                if ("true".equalsIgnoreCase(isSucc)) {
                    List<BizOrder> bizOrderList = bizOrderResponse.getOrderFieldsesList();
                    mHistoryTripsView.renderDataOnSuccess(bizOrderList);
                } else {
                    mHistoryTripsView.renderDataOnFailure(commonRespParams.getResultMsg());
                }
            }
        });
    }


    @Override
    public void commentOnDriver(String orderId, int starAmount, String commentMessage) {

        final CommentDriverParam commentDriverParam = new CommentDriverParam(
                getMobile(),
                getToken(),
                orderId,
                starAmount,
                commentMessage
        );
        mCommentDriverUseCase.execute(commentDriverParam, new ZMSubscriber<CommentDriverResponse>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
                mCommentOnDriverView.closeCommentOnCompleted();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mCommentOnDriverView.renderCommentOnError("");
            }

            @Override
            public void onNext(CommentDriverResponse commentDriverResponse) {
                super.onNext(commentDriverResponse);
                if (commentDriverResponse != null) {
                    boolean succ = commentDriverResponse.isSucc();
                    String resultMessage = commentDriverResponse.getResultMessage();
                    if (succ) {
                        mCommentOnDriverView.renderCommentOnSuccess();
                    } else {
                        mCommentOnDriverView.renderCommentOnError(resultMessage);
                    }
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        mCommentDriverUseCase.unsubscribe();
        mHistoryTripOrdersUseCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        if (view instanceof HistoryTripsView) {
            mHistoryTripsView = (HistoryTripsView) view;
        }
        if (view instanceof CommentOnDriverView) {
            mCommentOnDriverView = (CommentOnDriverView) view;
        }
        if (view instanceof UpPullRefreshView) {
            mUpPullRefreshView = (UpPullRefreshView<BizOrder>) view;
        }
    }


}
